package top.heue.utils.base.util.view

import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import top.heue.utils.base.helper.item.ItemDecoration

/** 设置边距，单位dp*/
fun RecyclerView.setMargin(margin: Int, columns: Int = 1) {
    addItemDecoration(ItemDecoration(margin, columns))
}

/** 设置边距，单位dp*/
fun RecyclerView.setMargin(
    marginLeft: Int,
    marginRight: Int,
    marginTop: Int,
    marginBottom: Int,
    columns: Int = 1
) {
    addItemDecoration(
        ItemDecoration(marginLeft, marginRight, marginTop, marginBottom, columns)
    )
}

fun RecyclerView.scrollToTop() {
    val layoutManager = this.layoutManager as LinearLayoutManager?
    layoutManager?.scrollToPositionWithOffset(0, 0)
}

fun RecyclerView.scrollToBottom() {
    val layoutManager = this.layoutManager as LinearLayoutManager?
    val itemCount = this.adapter?.itemCount ?: 0
    layoutManager?.scrollToPositionWithOffset(itemCount - 1, 0)
}